module SystemInfo
  class Info
    attr_accessor :env, :browser, :platform
    
    def initialize(request)
      self.env = request
      get_platform
      get_browser
    end
    
    protected
    # 获取系统平台
    def get_platform
      begin
        if self.env['HTTP_USER_AGENT'] =~ /Win/i
          self.platform = "Windows"
        elsif self.env['HTTP_USER_AGENT'] =~ /Mac/i
          self.platform = "Mac"
        elsif self.env['HTTP_USER_AGENT'] =~ /BSD/i
          self.platform = "FreeBSD";
        elsif self.env['HTTP_USER_AGENT'] =~ /Linux/i
          self.platform = "Linux";
        elsif self.env['HTTP_USER_AGENT'] =~ /SunOS/i
          self.platform = "Sun Solaris";
        elsif  self.env["HTTP_USER_AGENT"] =~ /iPhone/i
          self.platform = "iPhone";
        else
          self.platform = nil
        end
      rescue
        self.platform = nil
      end
    end
    
    # 获取浏览器信息  
    def get_browser  
      self.browser = {:type => nil, :version => nil}  
      begin  
        # Internet Exlorer  
        if self.env['HTTP_USER_AGENT'] =~ /MSIE/i && self.env['HTTP_USER_AGENT'].scan(/AOL|America Online Browser/i).empty?  
          browser[:type] = "MSIE";  
          browser[:version] = self.env['HTTP_USER_AGENT'].scan(/MSIE ([\d\.]+)/i).to_s  
          # Firefox/Firebird/Phoenix  
        elsif self.env['HTTP_USER_AGENT'] =~ /Firefox|Firebird|Phoenix/i  
          browser[:type] = "Firefox";  
          browser[:version] = self.env['HTTP_USER_AGENT'].scan(/[Firefox|Firebird|Phoenix].\/(\d.+)/i).to_s  
          # Galeon  
        elsif self.env['HTTP_USER_AGENT'] =~ /Galeon/i  
          browser[:type] = "Galeon";  
          browser[:version] = self.env['HTTP_USER_AGENT'].scan(/Galeon\/([\d\.]+)/i).to_s  
          # Safari  
        elsif self.env['HTTP_USER_AGENT'] =~ /Safari/i  
          browser[:type] = "Safari";  
          browser[:version] = '0'  
          # Opera  
        elsif self.env['HTTP_USER_AGENT'] =~ /Opera/i  
          browser[:type] = "Opera";  
          browser[:version] = self.env['HTTP_USER_AGENT'].scan(/Opera[ |\/]([\d\.]+)/i).to_s  
          # AOL/America Online Browser  
        elsif self.env['HTTP_USER_AGENT'] =~ /AOL|America Online Browser/i  
          browser[:type] = "AOL"  
          browser[:version] = if self.env['HTTP_USER_AGENT'] =~ /AOL/i  
            self.env['HTTP_USER_AGENT'].scan(/AOL[ |\/]([\d.]+)/i).uniq.to_s  
          else  
            self.env['HTTP_USER_AGENT'].scan(/America Online Browser ([\d\.]+)/i).to_s  
          end  
          # Camino  
        elsif self.env['HTTP_USER_AGENT'] =~ /Camino/i  
          browser[:type] = "Camino";  
          browser[:version] = self.env['HTTP_USER_AGENT'].scan(/Camino\/([\d\.]+)/i).to_s  
          # Konqueror  
        elsif self.env['HTTP_USER_AGENT'] =~ /Konqueror/i  
          browser[:type] = "Konqueror";  
          browser[:version] = self.env['HTTP_USER_AGENT'].scan(/Konqueror\/([\d.]+)/i).to_s  
          # K-Meleon  
        elsif self.env['HTTP_USER_AGENT'] =~ /K-Meleon/i  
          browser[:type] = "K-Meleon";  
          browser[:version] = self.env['HTTP_USER_AGENT'].scan(/K-Meleon\/([\d.]+)/i).to_s  
          # Firefox BonEcho  
        elsif self.env['HTTP_USER_AGENT'] =~ /BonEcho/i  
          browser[:type] = "Firefox BonEcho";  
          browser[:version] = self.env['HTTP_USER_AGENT'].scan(/BonEcho\/([\d.]+)/i).to_s  
          #Netscape  
        elsif self.env['HTTP_USER_AGENT'] =~ /Netscape/i  
          browser[:type] = "Netscape";  
          browser[:version] = self.env['HTTP_USER_AGENT'].scan(/Netscape\/([\d.]+)/i).to_s  
          # PSP  
        elsif self.env['HTTP_USER_AGENT'] =~ /PlayStation Portable/i  
          browser[:type] = "PlayStation Portable (PSP)";  
          browser[:version] = self.env['HTTP_USER_AGENT'].scan(/PlayStation Portable\); ([\d\.]+)/i).to_s  
          # PlayStation 3  
        elsif self.env['HTTP_USER_AGENT'] =~ /PlayStation 3/i  
          browser[:type] = "PlayStation 3";  
          browser[:version] = self.env['HTTP_USER_AGENT'].scan(/PlayStation 3; ([\d\.]+)/i).to_s  
          # Lynx  
        elsif self.env['HTTP_USER_AGENT'] =~ /Lynx/i  
          browser[:type] = "Lynx";  
          browser[:version] = self.env['HTTP_USER_AGENT'].scan(/Lynx\/([\d\.]+)/i).to_s  
        else  
          browser[:type] = nil  
          browser[:version] = nil  
        end  
      rescue  
        browser[:type] = nil  
        browser[:version] = nil  
      end  
    end  
  end
  
end  